﻿using DBLayer;
using DBLayer.Datacontext;
using ModelLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CtrLayer
{
    public class CtrAchievement : ICtrAchievement
    {
        /// <summary>
        /// Creates an achievement in the database.
        /// </summary>
        /// <param name="myUser">My user.</param>
        /// <param name="myAchievement">My achievement.</param>
        /// <returns>System.Int32.</returns>
        public int CreateAchievement(User myUser, Achievement myAchievement)
        {
            var dbAchievement = new DbAchievementOp();
            var dbUserAchievement = new DbUserAchievementOp();

            //add the given user as the achievement starter
            myAchievement.AchievementStarterId = myUser.UserId;

            //Insert the achievement and return the id
            int achId = dbAchievement.InsertAchievement(myAchievement);

            //Use the achievementId from the db on the local achievement
            myAchievement.AchievementId = achId;

            //Create a new user achievement and insert it in the database
            UserAchievement uach = new UserAchievement
                                   {
                                       User = myUser,
                                       Achievement = myAchievement,
                                       CurrentStatus = UserAchievement.Status.InProgress
                                   };

            //Return a positive number if success
            return dbUserAchievement.InsertUserAchievement(uach);
        }

        /// <summary>
        /// Gets the number of participants on a given achievement.
        /// </summary>
        /// <param name="achievement">The achievement.</param>
        /// <returns>System.Int32.</returns>
        public int GetAchievementUserCount(Achievement achievement)
        {
            var dbAchievement = new DbAchievementOp();
            return dbAchievement.GetAchievementUserList(achievement).Count;
        }

        /// <summary>
        /// Gets the user achievements related to a given user.
        /// </summary>
        /// <param name="myUser">My user.</param>
        /// <returns>List{UserAchievement}.</returns>
        public List<UserAchievement> GetUserAchievements(User myUser)
        {
            var dbUserAchievement = new DbUserAchievementOp();
            List<UserAchievement> returnList = dbUserAchievement.GetUserAchievements(myUser);
            return returnList;
        }

        public int SetUserAchievementStatus(UserAchievement userAchievement)
        {
            var dbUserAchievement = new DbUserAchievementOp();
            int result = dbUserAchievement.SetUserAchievementStatus(userAchievement);
            return result;
        }

        public int SetAchievementStatus(Achievement achievement)
        {
            var dbAchievementOp = new DbAchievementOp();
            int result = dbAchievementOp.SetAchievementStatus(achievement);
            return result;
        }

        public UserAchievement GetUserAchievement(int userId, int achId)
        {
            var dbAchievementOp = new DbAchievementOp();
            UserAchievement result = dbAchievementOp.GetUserAchievement(userId, achId);

            return result;
        }
    }
}